﻿using Dapper;
using Dapper_Core.BLL.Interface;
using Dapper_Core.Common;
using Dapper_Core.DAL.Impl;
using Dapper_Core.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Dapper_Core.BLL.Impl
{
    public class SideMenuBusiness : BaseRepository<SideMenuTreeNode>, ISideMenuBusiness
    {
        public SideMenuBusiness()
        {

        }

        public SideMenuBusiness(TransactionContext transactionContext) : base(transactionContext)
        {

        }

        /// <summary>
        /// 获取侧边菜单资源
        /// </summary>
        /// <param name="userCode">先根据userCode进行查询</param>
        /// <param name="roleCode">根据userCode查不到菜单资源时再根据roleCode查询</param>
        /// <returns></returns>
        public List<SideMenuTreeNode> GetSideMenus(string userCode, string roleCode)
        {
            List<SideMenuTreeNode> treeNodes = this.GetSideMenusByUser(userCode);
            if (treeNodes.Count < 1)
            {
                treeNodes = this.GetSideMenusByRole(roleCode);
            }
            return GetChildren(treeNodes, "0");
        }

        public List<SideMenuTreeNode> GetSideMenusByUser(string userCode)
        {
            try
            {
                if (string.IsNullOrEmpty(userCode))
                {
                    return new List<SideMenuTreeNode>();
                }
                Dictionary<string, string> dict = new Dictionary<string, string>();
                dict.Add("userCode", userCode);
                return base.QueryMap<SideMenuTreeNode>("GetSideMenusByUser", dict).ToList();
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "根据userCode获取侧边菜单信息失败", "userCode=" + userCode, ex, "DapperCore");
                return new List<SideMenuTreeNode>();
            }
        }

        public List<SideMenuTreeNode> GetSideMenusByRole(string roleCode)
        {
            try
            {
                if (string.IsNullOrEmpty(roleCode))
                {
                    return new List<SideMenuTreeNode>();
                }
                Dictionary<string, string> dict = new Dictionary<string, string>();
                dict.Add("roleCode", roleCode);
                return base.QueryMap<SideMenuTreeNode>("GetSideMenusByRole", dict).ToList();
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "根据roleCode获取侧边菜单信息失败", "roleCode=" + roleCode, ex, "DapperCore");
                return new List<SideMenuTreeNode>();
            }
        }

        private List<SideMenuTreeNode> GetChildren(List<SideMenuTreeNode> treeNodes, string parentCode)
        {
            List<SideMenuTreeNode> children = treeNodes.Where(a => a.ParentCode == parentCode).OrderBy(a => a.id).ToList();
            foreach (SideMenuTreeNode child in children)
            {
                child.children = GetChildren(treeNodes, child.id);
            }
            return children;
        }
    }
}